package azcat.planner.tools;

import java.util.ArrayList;
import java.util.Calendar;

import azcat.planner.materials.Material;
import azcat.planner.schedule.Schedule;

public class NeededToolsEstimator {
	
	private ArrayList<Tool> neededTools;
	private ArrayList<Material> materials;
	public NeededToolsEstimator(ArrayList<Tool> neededTools, ArrayList<Material> materials){
		this.neededTools = neededTools;
		this.materials = materials;
	}
	
	public void estimateSchedule(){		
		for(int i=0; i< neededTools.size(); i++){			
			//neededTools.get(i).clearSchedule();//clear whatever was read from the doc			
			neededTools.get(i).setSchedule(assignDaysToWork(neededTools.get(i), materials.get(i))); //assign the days the tool will be used for this project
		}		
	}
	
	private ArrayList<Schedule> assignDaysToWork(Tool t, Material m){
		ArrayList<Schedule> s = t.getSchedule();
		ArrayList<Schedule> newSchedule = new ArrayList<Schedule>();
		Calendar todayDate = Calendar.getInstance();		
		int hoursleft, mats;
		int total = m.getTotalComps();
		if(s.size()<=0){//if the tool is not being used at all then get the pieces as much as you can			
			mats = t.getProductivityPerHour() * 8; //calculate the pieces that can be produced in one day
			total = (int)(total/mats);
			//System.out.println(total);
			for(int i = 0; i< total; i++){
				Schedule sch = new Schedule();
				sch.setTheCalendar(todayDate, i);//adds one day everytime since the tool is not being used
				sch.setMatsPerDay(mats);
				//sch.printDate();
				newSchedule.add(sch);
			}			
			
		}
		else{	
			int i =0;
			Schedule temp = new Schedule();			
			while(total > 0){ //while there are still pieces to be made
				temp.setTheCalendar(todayDate, i);				
				if(hasDate(s, temp.getCalendar())){//if the tools is being used today					
					//see how many pieces can be made
					hoursleft = 8 - s.get(getIndexOfSchedule(s, temp.getCalendar())).getUsageHours();
					//System.out.println(hoursleft);
					if(hoursleft > 0){//if you can accomodate more tasks today
						mats  =t.getProductivityPerHour() * hoursleft;
						if(total <= mats){
							Schedule sch = new Schedule();
							sch.setTheCalendar(todayDate, i);
							sch.setMatsPerDay(total);
							//sch.printDate();
							newSchedule.add(sch);
							i++;
							total -= mats;
						}
						else{
							total -= mats;					
							
							Schedule sch = new Schedule();
							sch.setTheCalendar(todayDate, i);
							sch.setMatsPerDay(mats);
							//sch.printDate();
							newSchedule.add(sch);
							i++;
						}
					}
					else{
						i++;
					}
					
				}
				else{
					mats  =t.getProductivityPerHour() * 8; // can use all day since nothing is used
					if(total <= mats){
						Schedule sch = new Schedule();
						sch.setTheCalendar(todayDate, i);
						sch.setMatsPerDay(total);
						//sch.printDate();
						newSchedule.add(sch);
						i++;
						total -= mats;
					}
					else{
						total -= mats;
						
						Schedule sch = new Schedule();
						sch.setTheCalendar(todayDate, i);
						sch.setMatsPerDay(mats);
						//sch.printDate();
						newSchedule.add(sch);
						i++;
					}
				}
			}
			
		}
		return newSchedule;
	}
	
	private boolean hasDate(ArrayList<Schedule> s, Calendar c){
		for(int i=0; i< s.size(); i++){
			if(s.get(i).compareDates(s.get(i).getCalendar(), c)){
				return true;
			}			
		}
		return false;
	}
	
	private int getIndexOfSchedule(ArrayList<Schedule> s, Calendar c){
		for(int i=0; i< s.size(); i++){
			if(s.get(i).compareDates(s.get(i).getCalendar(), c)){
				return i;
			}			
		}
		return -1;
	}
}
